(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.bu.createContinueController', createContinueController);
	createContinueController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService', 'buService' ];
	function createContinueController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, buService) {
		var ctrl = this, buInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.bu = {};
		
		ctrl.init = function() {
			var params = {
//					dataKey : buInfo.dataKey,
//					dataStatus : buInfo.dataStatus,
					taskId : buInfo.taskId
			};
			ctrl.childInnerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.child.tableConfigs.table, childRowSelectionChanged);
			ctrl.childInnerGridOptions.data = 'vm.bu.buChilds';
			//
			ctrl.ctrlInnerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.ctrl.tableConfigs.table, ctrlRowSelectionChanged);
			ctrl.ctrlInnerGridOptions.data = 'vm.bu.buCtrls';
			//
			buService.initOtherData($scope, ctrl);
			authService.createContinue(params).then(function(response) {
				ctrl.bu = response.data.model;
				ctrl.bu.taskId = response.data.task.taskId || buInfo.taskId;
				ctrl.bu.buChilds = ctrl.bu.buChilds || [];
				ctrl.bu.buCtrls = ctrl.bu.buCtrls || [];
				ctrl.bu.changeBuType = ctrl.bu.changeBuType || "N";
				//
				ctrl.bu.oldBuType = ctrl.bu.buType;
				//
				buService.getBuTypeOptions().then(function(response) {
					ctrl.buTypeOptions = response;
					// need init currBuCtrlCat/currAllowChild/currBuCtrlProdFlag
					if (ctrl.bu.buType) {
						for (var i = 0; i < ctrl.buTypeOptions.length; i++) {
							var _buType = ctrl.buTypeOptions[i];
							if (_buType.value == ctrl.bu.buType) {
								ctrl.bu.currBuCtrlCat = _buType.extProperty.buCtrlCat;
								ctrl.bu.currAllowChild = _buType.extProperty.allowChild;
								ctrl.bu.currBuCtrlProdFlag = _buType.extProperty.buCtrlProdFlag;
								buService.changeBuCtrlTabTitle($scope, ctrl);
							}
						}
					}
				}, angular.noop);
			});
		};

		ctrl.createContinueSave = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else {
				return authService.createContinueSave(ctrl.bu).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.createContinueSubmit = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else {
				return authService.createContinueSubmit(ctrl.bu).then(submitSuccessful, submitFailed);
			}
		};
		
		ctrl.createContinueCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.bu.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createContinueCancel(ctrl.bu).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};
		
		function childRowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedBuChilds = selectedRows;
		}
		
		function ctrlRowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedBuCtrls = selectedRows;
		}

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.bu.list',
					'state' : 'cda.bu.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				angular.merge(ctrl.bu, response.data.model);
			} else if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.bu.list',
					'state' : 'cda.bu.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

	}
})(angular);
